package be.vdab.Servlets;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.dao.SauzenDAO;
import be.vdab.entities.Saus;

/**
 * Servlet implementation class SausenIngredienten
 */
@WebServlet("/sauzen/ingredienten.htm")
public class SauzenIngredientenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static String VIEW = "/WEB-INF/JSP/sauzeningredienten.jsp";
    private SauzenDAO sauzenDAO = new SauzenDAO();
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ingredient = request.getParameter("ingredient");
		if (ingredient != null){
			Map<String, String> fouten = new HashMap<>(); 
	        if (ingredient.isEmpty()) {
	        	fouten.put("ingredient", "verplicht");
	        } else {
	        	Collection<Saus> sauzen = sauzenDAO.findByIngredient(ingredient);
	        	if (sauzen.isEmpty()){
	        		fouten.put("nietgevonden", "geen sauzen gevonden");
	        	}
	        	request.setAttribute("sauzen", sauzen);
	        }
	        request.setAttribute("fouten", fouten);
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
		dispatcher.forward(request, response);
	}

	
}
